Imports System.Text
Imports IICBusinessLogic.PeriodProcessing
Imports org.model.lib
Imports IICBusinessLogic

<TestClass()>
Public Class TestAlternativeLessonId

    <TestMethod()>
    Public Sub testAlternativeLessonId()

        ModelContext.newForUnitTests()

        Dim maxPeriodDate As Date = _
            ModelContext.CurrentDBUtils.getDtValue("select max(periodDate) from [Period]")

        Try
            ModelContext.beginTrans()
            PeriodProcessorTestUtils.initialize()
            'create a diploma with 1 certificate
            Dim diploma As DiplomaType = PeriodProcessorTestUtils.createDiplomaWithXCertReq(1)

            'create 2 lessons
            Dim lessons As List(Of Lesson) = New List(Of Lesson)
            For i As Integer = 1 To 2
                lessons.Add(PeriodProcessorTestUtils.CreateLesson(100, 70))
            Next

            LessonDataUtils.saveLesson(lessons.ToArray)

            'add lesson 0 as a requirement 
            diploma.DiplomaRequirements(0).Certification.AddLessonRequirement(lessons(0), False)
            'add lesson1 as an alternative to lesson 0
            diploma.DiplomaRequirements(0).Certification.CertificationRequirements(0).LessonIdAlternative = lessons(1).LessonId

            DiplomaTypeDataUtils.saveDiplomaType(diploma)

            'create student 
            Dim studentcnt As Integer = _
                ModelContext.CurrentDBUtils.getIntValue("select max(StudentId) from Student")

            studentcnt = studentcnt + 1
            Dim stud As Student = StudentFactory.Create
            stud.FirstName = "t58 " & studentcnt
            stud.LastName = "t58 " & studentcnt
            stud.IdNumber = "t58" & studentcnt

            StudentDataUtils.saveStudent(stud)

            'create a Period of classes
            Dim period As Period = PeriodFactory.Create
            period.DiplomaSuperTypeId = PeriodProcessorTestUtils.diplomaSuperType.DiplomaSuperTypeId
            period.PeriodDate = DateAdd(DateInterval.Month, 1, maxPeriodDate)
            period.PeriodCode = "59" & ModelContext.CurrentDBUtils.getIntValue("select count(*)+1 from period")
            Dim pername As String = period.PeriodName
            PeriodDataUtils.savePeriod(period)

            'Create a period class and assing the student to the alternative lesson id
            PeriodProcessorTestUtils.CreateClassAndAddStudentsAsClassMembers(lessons(1), period, stud)

            Dim perClass1 As PeriodClass = period.getClass(0) ' get first class in the collection
            Dim lsn As Lesson = perClass1.Lesson

            Dim passGrade As Integer = CInt(perClass1.PassGrade)

            'student passes lesson
            '------- EXPECTED OUTCOME: ------------ 
            'Student: earns 1 certificate, 1 dipoloma satisfied requirement
            Dim member As ClassMember = perClass1.getMember(0)
            member.Attend = True
            member.Grade = perClass1.PassGrade + 10 'student passed

            PeriodClassDataUtils.savePeriodClass(perClass1)

            PeriodDataUtils.savePeriod(period) 'save again!

            Dim dc As PeriodProcessor = New PeriodProcessor(period)
            dc.processPeriodAndStoreToDb()
            Assert.AreEqual(1, dc.certificatesEarned.Count, "CertificatesEarned.Count")

            'Student 1 passed alternative lesson, so 1 diploma earned
            Assert.AreEqual(1, dc.diplomasEarned.Count, "DiplomasEarned.Count")
            Assert.AreEqual(1, stud.SatisifiedDiplomaRequirements(diploma.DiplomaTypeId).Count)
            Assert.AreEqual(1, dc.membersProcessedCount)


            ModelContext.rollbackTrans()
        Finally
            ModelContext.rollbackTrans()
        End Try

    End Sub

End Class
